﻿using System;
using System.Collections.Generic;
using System.Text;

using NAnt.Core;
using NAnt.Core.Attributes;

namespace ReferenceSwap.Nant
{
    public enum ReferenceType
    {
        Project,
        Assembly,
        Match
    }

    [ElementName("ref")]
    public class ReferenceElement : Element
    {
        [TaskAttribute("type")]
        public ReferenceType Type { get; set; }

        [TaskAttribute("value")]
        [StringValidator(AllowEmpty = false)]
        public string Value { get; set; }

        public override bool Equals(object other)
        {
            if (other is ReferenceElement)
            {
                return ((ReferenceElement)other).Type == Type && ((ReferenceElement)other).Value == Value;
            }
            else if (other is string)
            {
                return Value == (string)other;
            }

            return false;
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
    }

    public class ReferenceCollection : System.Collections.CollectionBase
    {
        public ReferenceCollection() { }

        public ReferenceCollection(ReferenceCollection coll)
        {
            InnerList.AddRange(coll);
        }

        public ReferenceElement this[int index]
        {
            get { return (ReferenceElement)List[index]; }
            set { List[index] = value; }
        }

        public virtual void Add(ReferenceElement reference)
        {
            if (!List.Contains(reference))
            {
                List.Add(reference);
            }
        }

        public bool Contains(object o)
        {
            return List.Contains(o);
        }

        protected override void OnValidate(object value)
        {
            base.OnValidate(value);
            if (!(value is ReferenceElement))
            {
                throw new ArgumentException("Collection only supports ReferenceElement objects.");
            }
        }
    }    
}
